home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Contributed / SpriteWorld / SpriteWorld Examples / Shark Attack / Sources & Headers / NewSprite.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-10-06  |  7.7 KB  |  254 lines  |  [TEXT/CWIE]

  1. #include <SWIncludes.h>
  2. #include <SWGameUtils.h>
  3. #include <SWApplication.h>
  4.  
  5. #include "Application.h"
  6. #include "Shark Attack.h"
  7. #include "SpriteMoveProcs.h"
  8. #include "NewSprite.h"
  9. #include "SpriteCollideProcs.h"
  10. #include "Stats.h"
  11. #include "GlobalVariables.h"
  12.  
  13.  
  14.     // Pointers to the original sprites which we clone
  15.     // when we want to add a sprite to the animation.
  16.     // We never add these sprites themselves to the animation.
  17.     
  18. SpritePtr    gMasterSubSpriteP,
  19.             gMasterBulletSpriteP,
  20.             gMasterFishSpriteP,
  21.             gMasterSharkSpriteP,
  22.             gMasterTitleSpriteP;
  23.  
  24.  
  25. #define        kFishStrength    2        // Number of shots it takes to kill a fish
  26. #define     kSharkStrength    15        // Number of shots it takes to kill a shark
  27.  
  28.  
  29. ///--------------------------------------------------------------------------------------
  30. // LoadSprites
  31. ///--------------------------------------------------------------------------------------
  32.  
  33. void    LoadSprites( void )
  34. {
  35.     OSErr            err;
  36.     
  37.         // Load gMasterSubSpriteP
  38.     err = SWCreateSpriteFromCicnResource(gSpriteWorldP, &gMasterSubSpriteP, 
  39.             NULL, 128, 2, kFatMask);
  40.     FatalError(err);
  41.     SetUpSprite(gMasterSubSpriteP);
  42.     
  43.         // Load gMasterBulletSpriteP
  44.     err = SWCreateSpriteFromCicnResource(gSpriteWorldP, &gMasterBulletSpriteP, 
  45.             NULL, 130, 1, kFatMask);
  46.     FatalError(err);
  47.     SetUpSprite(gMasterBulletSpriteP);
  48.     
  49.         // Load gMasterFishSpriteP
  50.     err = SWCreateSpriteFromPictResource(gSpriteWorldP, &gMasterFishSpriteP, 
  51.             NULL, 300, 300, 6, kFatMask);
  52.     FatalError(err);
  53.     SetUpSprite(gMasterFishSpriteP);
  54.  
  55.         // Load gMasterSharkSpriteP
  56.     err = SWCreateSpriteFromPictResource(gSpriteWorldP, &gMasterSharkSpriteP, 
  57.             NULL, 200, 200, 6, kFatMask);
  58.     FatalError(err);
  59.     SetUpSprite(gMasterSharkSpriteP);
  60.     
  61.         // Load gMasterTitleSpriteP
  62.     err = SWCreateSpriteFromPictResource(gSpriteWorldP, &gMasterTitleSpriteP, 
  63.             NULL, 128, 128, 1, kFatMask);
  64.     FatalError(err);
  65.     SetUpSprite(gMasterTitleSpriteP);
  66. }
  67.  
  68.  
  69. ///--------------------------------------------------------------------------------------
  70. // DisposeSprites - we must dispose of each sprite we loaded, since they are not
  71. // added to the SpriteWorld, and therefore will not be disposed with the SpriteWorld.
  72. ///--------------------------------------------------------------------------------------
  73.  
  74. void    DisposeSprites( void )
  75. {
  76.     SWDisposeSprite(&gMasterSubSpriteP);
  77.     SWDisposeSprite(&gMasterBulletSpriteP);
  78.     SWDisposeSprite(&gMasterFishSpriteP);
  79.     SWDisposeSprite(&gMasterSharkSpriteP);
  80.     SWDisposeSprite(&gMasterTitleSpriteP);
  81. }
  82.  
  83.  
  84. ///--------------------------------------------------------------------------------------
  85. // SetUpSprite - makes the LoadSprites code a little cleaner.
  86. ///--------------------------------------------------------------------------------------
  87.  
  88. void    SetUpSprite(SpritePtr mySpriteP)
  89. {
  90.     Rect    moveBoundsRect;
  91.     
  92.     if (gSpriteWorldP->pixelDepth >= 8)
  93.         SWCompileSprite(mySpriteP);
  94.     
  95.         // we have to do this since the game rect is smaller than the title screen rect
  96.     moveBoundsRect = gSpriteWorldP->backRect;
  97.     moveBoundsRect.bottom -= kStatsHeight;
  98.     SWSetSpriteMoveBounds(mySpriteP, &moveBoundsRect);
  99.     SWSetSpriteDrawProc(mySpriteP, gSpriteMaskDrawProc);
  100.     SWLockSprite(mySpriteP);
  101. }
  102.  
  103.  
  104. ///--------------------------------------------------------------------------------------
  105. // NewSubSprite - clone the master sprite and add the clone to the animation.
  106. // Also allocate memory for the custom sprite structure.
  107. ///--------------------------------------------------------------------------------------
  108.  
  109. SpritePtr    NewSubSprite( void )
  110. {
  111.     SubStructPtr    subStructP;
  112.     SpritePtr        subSpriteP;
  113.     OSErr            err;
  114.     
  115.         // Allocate memory for the SubStruct
  116.     subStructP = (SubStructPtr)NewPtr(sizeof(SubStruct));
  117.     FatalError( MemError() );
  118.  
  119.     err = SWCloneSprite(gMasterSubSpriteP, &subSpriteP, subStructP);
  120.     FatalError(err);
  121.     
  122.     SWAddSprite(gSubSpriteLayerP, subSpriteP);
  123.     SWSetSpriteMoveProc(subSpriteP, SubSpriteMoveProc);
  124.     SWSetSpriteCollideProc(subSpriteP, SubSpriteCollideProc);
  125.     
  126.     subStructP->horizDelta = 0;
  127.     subStructP->vertDelta = 0;
  128.     subStructP->horizPos = gSpriteWorldP->backRect.right/2-20;
  129.     subStructP->vertPos = gSpriteWorldP->backRect.bottom/2-20;
  130.     
  131.     subStructP->curDirection = kLeftDirection;
  132.     subStructP->numBulletsOnScreen = 0;
  133.     subStructP->nextShotDelay = 0;
  134.     subStructP->canShoot = true;
  135.     
  136.     return subSpriteP;
  137. }
  138.  
  139.  
  140. ///--------------------------------------------------------------------------------------
  141. // NewBulletSprite - clone the master sprite and add the clone to the animation.
  142. // Also allocate memory for the custom sprite structure.
  143. ///--------------------------------------------------------------------------------------
  144.  
  145. SpritePtr    NewBulletSprite( void )
  146. {
  147.     BulletStructPtr    bulletStructP;
  148.     SpritePtr        bulletSpriteP;
  149.     OSErr            err;
  150.     
  151.         // Allocate memory for the BulletStruct
  152.     bulletStructP = (BulletStructPtr)NewPtr(sizeof(BulletStruct));
  153.     FatalError( MemError() );
  154.  
  155.     err = SWCloneSprite(gMasterBulletSpriteP, &bulletSpriteP, bulletStructP);
  156.     FatalError(err);
  157.     
  158.     SWAddSprite(gBulletSpriteLayerP, bulletSpriteP);
  159.     SWSetSpriteMoveProc(bulletSpriteP, BulletSpriteMoveProc);
  160.  
  161.     return bulletSpriteP;
  162. }
  163.  
  164.  
  165. ///--------------------------------------------------------------------------------------
  166. // NewFishSprite - clone the master sprite and add the clone to the animation.
  167. // Also allocate memory for the custom sprite structure.
  168. ///--------------------------------------------------------------------------------------
  169.  
  170. SpritePtr    NewFishSprite( void )
  171. {
  172.     FishStructPtr    fishStructP;
  173.     SpritePtr        fishSpriteP;
  174.     OSErr            err;
  175.     
  176.         // Allocate memory for the FishStruct
  177.     fishStructP = (FishStructPtr)NewPtr(sizeof(FishStruct));
  178.     FatalError( MemError() );
  179.  
  180.     err = SWCloneSprite(gMasterFishSpriteP, &fishSpriteP, fishStructP);
  181.     FatalError(err);
  182.     
  183.     SWAddSprite(gFishSpriteLayerP, fishSpriteP);
  184.     SWSetSpriteMoveProc(fishSpriteP, FishSpriteMoveProc);
  185.     SWSetSpriteCollideProc(fishSpriteP, FishSpriteCollideProc);
  186.     SWSetSpriteFrameTime(fishSpriteP, 1000/15);
  187.     SWSetSpriteFrameAdvanceMode(fishSpriteP, kSWPatrollingMode);
  188.     
  189.     fishStructP->energy = kFishStrength;
  190.     gNumFishOnScreen++;
  191.     fishStructP->moveDelay = 0;
  192.     fishStructP->hitCounter = 0;
  193.  
  194.     return fishSpriteP;
  195. }
  196.  
  197.  
  198. ///--------------------------------------------------------------------------------------
  199. // NewSharkSprite - clone the master sprite and add the clone to the animation.
  200. // Also allocate memory for the custom sprite structure.
  201. ///--------------------------------------------------------------------------------------
  202.  
  203. SpritePtr    NewSharkSprite( void )
  204. {
  205.     SharkStructPtr    sharkStructP;
  206.     SpritePtr        sharkSpriteP;
  207.     OSErr            err;
  208.     
  209.         // Allocate memory for the SharkStruct
  210.     sharkStructP = (SharkStructPtr)NewPtr(sizeof(SharkStruct));
  211.     FatalError( MemError() );
  212.  
  213.     err = SWCloneSprite(gMasterSharkSpriteP, &sharkSpriteP, sharkStructP);
  214.     FatalError(err);
  215.     
  216.     SWAddSprite(gSharkSpriteLayerP, sharkSpriteP);
  217.     SWSetSpriteMoveProc(sharkSpriteP, SharkSpriteMoveProc);
  218.     SWSetSpriteCollideProc(sharkSpriteP, SharkSpriteCollideProc);
  219.     SWSetSpriteFrameTime(sharkSpriteP, 1000/5);
  220.     SWSetSpriteFrameAdvanceMode(sharkSpriteP, kSWPatrollingMode);
  221.     
  222.     sharkStructP->horizDelta = 0;
  223.     sharkStructP->vertDelta = 0;
  224.     
  225.     sharkStructP->energy = kSharkStrength;
  226.     sharkStructP->moveDelay = 0;
  227.     sharkStructP->hitCounter = 0;
  228.     gNumSharksOnScreen++;
  229.  
  230.     return sharkSpriteP;
  231. }
  232.  
  233.  
  234. ///--------------------------------------------------------------------------------------
  235. // AddTitleSprite
  236. ///--------------------------------------------------------------------------------------
  237.  
  238. void    AddTitleSprite( void )
  239. {
  240.     SpritePtr        titleSpriteP;
  241.     Rect            titleSpriteRect;
  242.     OSErr            err;
  243.     
  244.     err = SWCloneSprite(gMasterTitleSpriteP, &titleSpriteP, NULL);
  245.     FatalError(err);
  246.     
  247.     SWAddSprite(gSubSpriteLayerP, titleSpriteP);
  248.     
  249.     titleSpriteRect = titleSpriteP->curFrameP->frameRect;
  250.     CenterRect(&titleSpriteRect, &gSpriteWorldP->backRect);
  251.     SWSetSpriteLocation(titleSpriteP, titleSpriteRect.left, titleSpriteRect.top);
  252. }
  253.  
  254.